package com.wx.activitys.drp.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wx.activitys.base.utils.Common;
import com.wx.activitys.drp.EnumUtils.EnumIncomeType;
import com.wx.activitys.drp.EnumUtils.EnumTream;
import com.wx.activitys.drp.Utils.DoubleUtil;
import com.wx.activitys.drp.mapper.BClickMapper;
import com.wx.activitys.drp.mapper.BUserIncomeMapper;
import com.wx.activitys.drp.mapper.DictionaryMapper;
import com.wx.activitys.drp.mapper.ProfitMapper;
import com.wx.activitys.drp.mapper.ShareMapper;
import com.wx.activitys.drp.mapper.UserInfoMapper;
import com.wx.activitys.drp.model.BClick;
import com.wx.activitys.drp.model.BDictionary;
import com.wx.activitys.drp.model.BProfit;
import com.wx.activitys.drp.model.BShare;
import com.wx.activitys.drp.model.BUserIncome;
import com.wx.activitys.drp.model.BUserInfo;
import com.wx.activitys.drp.service.BClickService;
@Service("/bClickService")
public class BClickServiceImpl implements BClickService {
   @Autowired
   private BClickMapper bClickMapper;
   @Autowired
   private UserInfoMapper userInfoMapper;
   @Autowired
   private ProfitMapper profitMapper;
   @Autowired
   private BUserIncomeMapper bUserIncomeMapper;
   @Autowired
   private ShareMapper shareMapper;
   @Autowired
   DictionaryMapper dictionaryMapper;
   
	@Override
	public int insert(BClick entity) {
		
		return bClickMapper.insert(entity);
	}

	@Override
	public List<BClick> findByParam(BClick entity) {
		
		return bClickMapper.findByParam(entity);
	}

	@Override
	public int update(BClick entity) {

		return bClickMapper.update(entity);
	}

	@Override
	public void updateOrSave(String openid, String platformId) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		BClick entity = new BClick();
		entity.setOpenid(openid);
		entity.setPlatfromId(platformId);
		entity.setYearMonth(sdf.format(new Date()));
		List<BClick> bClicks= bClickMapper.findByParam(entity);
		if(bClicks.size()>0){
			entity.setId(bClicks.get(0).getId());
			entity.setClickNum(bClicks.get(0).getClickNum()+1);				
			bClickMapper.update(entity);
		}else{
			entity.setCreateTime(new Date());
			entity.setClickNum(1);
			String clickId=Common.generateUUID();
			entity.setId(clickId);
			bClickMapper.insert(entity);
			BUserInfo bUserInfo=userInfoMapper.findByOpenid(openid);
			if(bUserInfo.getRegisterStatus()==0){
				Map<String,Object> map = new HashMap<>();
				if(StringUtils.isNotBlank(bUserInfo.getpId())){
					BDictionary entity1 = new BDictionary();
					//Map<String,Object> map = new HashMap<>();
					entity1.setCode("registered_share");
					BDictionary bi=dictionaryMapper.selectEntity(entity1);
					if(StringUtils.isNotBlank(bi.getValue())){
						Double income = Double.valueOf(bi.getValue());
						map.put("incomeadd", income);
						map.put("openid", bUserInfo.getpId());
						userInfoMapper.update(map);
						BUserIncome bu = new BUserIncome();
						bu.setId(Common.generateUUID());
						bu.setIncome(income);
						bu.setOpenid(bUserInfo.getpId());
						bu.setCreateTime(new Date());
						bu.setType(EnumIncomeType.注册.getValue());
						bUserIncomeMapper.insert(bu);	
					}
				}
				map.clear();
				map.put("registerStatus", "1");
				map.put("openid", bUserInfo.getOpenid());
				userInfoMapper.update(map);
			}
			
			/*//每次插入tb_click数据库并且算所得的分润
			BUserInfo userInfo =userInfoMapper.findByOpenid(openid);
			Map<String,Object> map = new HashMap<>();
			BUserIncome bu = new BUserIncome();
			//获取经销商等级及所得分润
			if(userInfo.getTreamRole().equals(EnumTream.经销商.getValue())){
				map.put("roleKey", userInfo.getRoleKey());
				map.put("paltformid", platformId);
				BProfit bp =profitMapper.findByMap(map);
				bu.setClickId(clickId);
				bu.setCreateTime(new Date());
				bu.setId(Common.generateUUID());
				bu.setType(EnumIncomeType.分销.getValue());
				bu.setIncome(bp.getValue());
				bu.setOpenid(openid);
				bUserIncomeMapper.insert(bu);	
			}else if(userInfo.getTreamRole().equals(EnumTream.分销商1级.getValue())){
				//分销商分润
				map.clear();
				map.put("openId", userInfo.getOpenid());
				map.put("platformid", platformId);
				BShare bs = shareMapper.findByMap(map);
				bu.setClickId(clickId);
				bu.setCreateTime(new Date());
				bu.setId(Common.generateUUID());
				bu.setType(EnumIncomeType.分销.getValue());
				bu.setIncome(bs.getValue());
				bu.setOpenid(openid);
				bUserIncomeMapper.insert(bu);
				//top分润
				map.put("roleKey", userInfo.getRoleKey());
				map.put("paltformid", platformId);
				BProfit bp =profitMapper.findByMap(map);
				bu.setIncome(DoubleUtil.sub(bs.getValue(),bp.getValue()));
				bu.setOpenid(userInfo.getpId());
				bUserIncomeMapper.insert(bu);
			}	*/
		}	
	}

	@Override
	public List<BClick> findclickNumByOpenid(Map<String, Object> map) {
		return bClickMapper.findclickNumByOpenid(map);
	}

	@Override
	public List<BClick> findMonthOrallClick(Map<String, Object> map) {
		return bClickMapper.findMonthOrallClick(map);
	}	
}
